﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using KpBusiness.Model;
using KpBusiness.Layer;
using NHibernate;
using NHibernate.Criterion;

namespace KpBusiness.Service
{
    public class PositionCategoryService
    {
        private static readonly ILog log = LogManager.GetLogger("Services");

        /// <summary>
        /// Get PositionCategory
        /// </summary>
        /// <param name="positionCategoryId">Provide PositionCategory Number</param>
        /// <returns>Returns PositionCategory</returns>
        public PositionCategory GetPositionCategory(int positionCategoryId)
        {
            log.Debug("Started");
            ISession session = DataAccessLayerHelper.OpenReaderSession();
            PositionCategory positionCategory = null;
            ITransaction transaction = null;
            try
            {
                transaction = session.BeginTransaction();
                positionCategory = session.Get<PositionCategory>(positionCategoryId);
                session.Flush();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                if (transaction != null && transaction.IsActive)
                    transaction.Rollback();
                log.Error(ex);

            }
            finally
            {
                if (transaction != null)
                    transaction.Dispose();

                if (session != null && session.IsConnected)
                    session.Close();
            }

            log.Debug("End");
            return positionCategory;
        }

        /// <summary>
        /// Creates New PositionCategory in DB
        /// </summary>
        /// <param name="positionCategory">Provide PositionCategory</param>
        public void SavePositionCategory(PositionCategory positionCategory)
        {
            log.Debug("Started");
            ISession session = DataAccessLayerHelper.OpenWriterSession();

            ITransaction transaction = null;
            try
            {
                transaction = session.BeginTransaction();
                session.SaveOrUpdate(positionCategory);
                session.Flush();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                if (transaction != null && transaction.IsActive)
                    transaction.Rollback();
                log.Error(ex);

            }
            finally
            {
                if (transaction != null)
                    transaction.Dispose();

                if (session != null && session.IsConnected)
                    session.Close();
            }

            log.Debug("End");

        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IList<PositionCategory> GetAllPositionCategorys()
        {

            log.Debug("Started");
            ISession session = DataAccessLayerHelper.OpenReaderSession();
            IList<PositionCategory> students = null;
            ITransaction transaction = null;
            try
            {
                transaction = session.BeginTransaction();

                ICriteria criteria = session.CreateCriteria(typeof(PositionCategory));
                students = criteria.List<PositionCategory>();
                session.Flush();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                if (transaction != null && transaction.IsActive)
                    transaction.Rollback();
                log.Error(ex);

            }
            finally
            {
                if (transaction != null)
                    transaction.Dispose();

                if (session != null && session.IsConnected)
                    session.Close();
            }

            log.Debug("End");
            return students;
        }

        /// <summary>
        /// Get All Active PositionCategorys
        /// </summary>
        /// <returns></returns>
        public IList<PositionCategory> GetAllActivePositionCategorys()
        {

            log.Debug("Started");
            ISession session = DataAccessLayerHelper.OpenReaderSession();
            IList<PositionCategory> students = null;
            ITransaction transaction = null;
            try
            {
                transaction = session.BeginTransaction();

                ICriteria criteria = session.CreateCriteria(typeof(PositionCategory))
                    .Add(Restrictions.Eq("PositionCategory.IsActive", true));

                students = criteria.List<PositionCategory>();
                session.Flush();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                if (transaction != null && transaction.IsActive)
                    transaction.Rollback();
                log.Error(ex);

            }
            finally
            {
                if (transaction != null)
                    transaction.Dispose();

                if (session != null && session.IsConnected)
                    session.Close();
            }

            log.Debug("End");
            return students;
        }
    }
}
